package com.taiyuan.zfmvp.JUC.ThreadPool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption:
 * @Date 2022/10/7
 * @return
 */
public class ThreadPoolDemo3 {

    public static void main(String[] args) throws Exception {
        List<String> listStr = new ArrayList<>();
        listStr.add("a");
        listStr.add("b");
        listStr.add("c");
        listStr.add("d");
        listStr.add("e");
        listStr.add("f");
        listStr.add("g");
        listStr.add("h");
        listStr.add("i");
        listStr.add("j");

        for (int i = 0; i < listStr.size(); i++) {
            if("f".equals(listStr.get(i)) || "g".equals(listStr.get(i))  ){
                 listStr.remove(i);
                 i--;
                continue;
            }
        }

        ExecutorService pool = ThreadPoolDemo2.Thread4Use2();
        long start = System.currentTimeMillis();
        final CountDownLatch countDownLatch = new CountDownLatch(listStr.size());//倒数计数器

        for (int i = 0; i < listStr.size(); i++) {

                final int j=i;
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            /*if(j == 5){
                                listStr.remove(j);
                                //System.out.println(j+"countDown");
                                //countDownLatch.countDown();
                                return;
                            }*/
                            Thread.sleep(1000);
                            System.out.println(Thread.currentThread().getName()+" 办理业务"+listStr.get(j));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            System.out.println(j+"countDown"+countDownLatch.getCount());

                            countDownLatch.countDown();
                            //System.out.println(j+"countDown"+countDownLatch.getCount());
                        }

                    }
                });

        }
        countDownLatch.await();
        pool.shutdown();

        long end = System.currentTimeMillis();
        System.out.println("执行批量插入 =======END，耗时=" + (end-start));

        /*long start = System.currentTimeMillis();
        for (int i = 0; i < 20; i++) {
            try {
                fangfa1(i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("执行批量插入 =======END，耗时=" + (end-start));*/

    }

    public static void fangfa1(int j) throws Exception{
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName()+" 办理业务"+j);
    }

}
